#include "DataBlock.h"
#include <string.h>

using namespace std;

DataBlock::DataBlock(int blockNumber, int blockOffset, int blockSize) : number(blockNumber),
			offset(blockOffset), blockSize(blockSize) {
	//data = new char (blockSize);
	_data.clear();
	for (int i=0; i<blockSize; i++) {
		_data.push_back(0);
	}
}

DataBlock::~DataBlock() {}

int DataBlock::getNumber () {
	return number;
}

void DataBlock::setData (unsigned char* newData) {
	//strncpy(data,newData,blockSize);
	for (int i=0; i<blockSize; i++) {
		_data[i] = newData[i];
	}
}

void DataBlock::getData (unsigned char buf[]) {
	for (int i=0; i<(int)_data.size(); i++) {
		buf[i] = _data[i];
	}
}

int DataBlock::getOffset() {
	return offset;
}

void DataBlock::writeToPhysicalDisk(int blockOffset, unsigned char* data) {

	/*
	int fd = open("FILE_SYS", O_WRONLY, 0666);

	//goes to the location on the inode:
	lseek(fd, blockOffset, SEEK_SET);

	//writes block`s data:
	int charSize = sizeof(char);
	unsigned char charData[blockSize];
	getData(charData);
	for (int i=0; i<blockSize; i++) {
		cout << charData[i];
	}
	cout << endl;
	write(fd, data, blockSize*charSize);

	//closes the file
	close(fd);
	*/
	DiskUtils::writeToDisk(data, blockOffset, blockSize*sizeof(char));
}

void DataBlock::readFromPhysicalDisk(int blockOffset, unsigned char* data) {
	DiskUtils::readFromDisk(data, blockOffset, blockSize*sizeof(char));
}
